#region Using Directives

using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;

using Microsoft.Practices.ComponentModel;
using Microsoft.Practices.RecipeFramework;

using EnvDTE;
using EnvDTE80;

#endregion

namespace EEst.SoftwareFactory.Actions
{
    /// <summary>
    /// the action to configure Makefile.xc and XCConfig.xml
    /// </summary>
    class GetXCConfigAction : Action
    {
        #region Input Properties

        #endregion

        #region Output Properties

        [Output]
        public string CygwinHome
        {
            get { return cygwinHome; }
            set { cygwinHome = value; }
        } string cygwinHome;

        [Output]
        public string BrickOSHome
        {
            get { return brickOSHome; }
            set { brickOSHome = value; }
        } string brickOSHome;

        [Output]
        public string H8ToolsHome
        {
            get { return h8ToolsHome; }
            set { h8ToolsHome = value; }
        } string h8ToolsHome;
        #endregion

        #region IAction Members

        public override void Execute()
        {
            DTE vs = (DTE)GetService<DTE>(true);
            Solution solution = vs.Solution;

            FileInfo fi = new FileInfo(solution.FullName);
            
            string configFile = fi.DirectoryName+ "\\RobotBase\\PathfinderRobot\\XCConfig.xml";

            XPathDocument doc = new XPathDocument(configFile);
            XPathNavigator navi = doc.CreateNavigator();
            XPathNodeIterator it = navi.Select("XCConfig/Variables/Variable");
            while (it.MoveNext())
            {
                string name = it.Current.GetAttribute("name",String.Empty);
                string value = it.Current.GetAttribute("value",String.Empty);
                if (name.CompareTo("CYGWIN_HOME") == 0)
                {
                    cygwinHome = value;
                }
                else if (name.CompareTo("BRICKOS_HOME") == 0)
                {
                    brickOSHome = value;
                }
                else if (name.CompareTo("XC_TOOLS_HOME") == 0)
                {
                    h8ToolsHome = value;
                }
            }
        }

        public override void Undo()
        {
       //     throw new Exception("The method or operation is not implemented.");
        }

        #endregion
    }
}
